package com.edmendst.xmall.controller;

import com.edmendst.xmall.dto.ShopInfo;
import com.edmendst.xmall.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;


//    //  创建店铺
//    int createShop(String username,String shopName);
    @PostMapping("/create/{shopName}")
    public int createShop(@PathVariable String shopName){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        return shopService.createShop(username, shopName);
    }

//    // 查询店铺信息
//    List<ShopInfo> selectShopInfoByUsername(String username);
    @RequestMapping("/select")
    public List<ShopInfo> selectShopInfoByUsername(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        return shopService.selectShopInfoByUsername(username);
    }


//    //     Integer opcode;  //0：修改店铺logo   1：修改店铺名称   2：修改店铺描述
//    int updateShopInfo(String username, ShopInfo shopInfo);
    @PostMapping("/shopInfo")
    public int updateShopInfo(@RequestBody ShopInfo shopInfo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        return shopService.updateShopInfo(username, shopInfo);
    }

//    //  修改店铺logo
//    int updateShopLogo(MultipartFile shopLogo, ShopInfo shopInfo);
    @PostMapping("/shopLogo")
    public int updateShopLogo( @RequestParam("shopLogo") MultipartFile shopLogo,
                               @RequestParam("shopId") Integer shopId){
        return shopService.updateShopLogo( shopLogo,  shopId);
    }

}
